import { TodoActionType, ToDoItem } from '..'

const initValue: ToDoItem[] = [
  {
    id: 1,
    name: '吃饭',
    isDone: false
  }
]
export default function todos (state = initValue, action: TodoActionType): ToDoItem[] {
  if (action.type === 'todo/UPDATE_STATE') {
    // 修改状态
    const id = action.payload
    return state.map(it => {
      if (it.id === id) {
        return { ...it, isDone: !it.isDone }
      } else {
        return it
      }
    })
  } else if (action.type === 'todo/ADD_STATE') {
    return [...state, action.payload]
  } else if (action.type === 'todo/INIT_STATE') {
    return action.initState
  }
  return state
}
